package com.example.ws.service.Impl;

import com.example.ws.service.IMessageRankService;
import com.example.ws.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MessageRankServiceImpl implements IMessageRankService {
    private static final String MESSAGE_RANK_KEY = "chat:message_rank";
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private IUserService userService;
    @Override
    public void incrementMessageCount(String userId) {
        redisTemplate.opsForZSet().incrementScore(MESSAGE_RANK_KEY,userId,1);
    }

    @Override
    public Set<ZSetOperations.TypedTuple<String>> getTopUsers() {
        return redisTemplate.opsForZSet().reverseRangeWithScores(MESSAGE_RANK_KEY,0,9);
    }


}
